package com.ysw.rpc.common.enums;

import com.ysw.rpc.common.messages.Message;
import com.ysw.rpc.common.messages.Request;
import com.ysw.rpc.common.messages.Response;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Objects;

/**
 * 1：Request、 2：Response
 *
 * @author YinShangwen
 * @since 2023/4/4 13:44
 */
@Getter
@AllArgsConstructor
public enum MessageTypeEnum {
    /**
     * Request
     */
    REQUEST(Request.class),
    /**
     * Response
     */
    RESPONSE(Response.class);

    private final Class<? extends Message> clazz;

    /**
     * 获取枚举对象---根据clazz
     *
     * @param clazz clazz
     * @return enum
     */
    public static MessageTypeEnum get(Class<? extends Message> clazz) {
        for (MessageTypeEnum value : values()) {
            if (Objects.equals(value.getClazz(), clazz)) {
                return value;
            }
        }
        return REQUEST;
    }
}
